<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Discussion.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: Monospaced}
pre {color: #000000; background-color: #ffffff; font-family: Monospaced}
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}
.ST0 {color: #969696; font-family: Monospaced; font-weight: bold}
.comment {color: #969696}
.line-number {background-color: #e9e8e2}
.ST1 {color: #ce7b00; font-family: Monospaced; font-weight: bold}
.character {color: #ce7b00}
.keyword-directive {color: #0000e6}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">C:\GoogleCode\bari\Bari\src\java\dk\jsh\itdiplom\dbsw\bari\wicket\Discussion.java</td></tr></table>
<pre>
<span class="line-number">  1</span> <span class="keyword-directive">package</span> dk.jsh.itdiplom.dbsw.bari.wicket;
<span class="line-number">  2</span> <span class="keyword-directive">import</span> dk.jsh.itdiplom.dbsw.bari.business.DiscussionMessageBusiness;
<span class="line-number">  3</span> <span class="keyword-directive">import</span> dk.jsh.itdiplom.dbsw.bari.domain.BariCase;
<span class="line-number">  4</span> <span class="keyword-directive">import</span> dk.jsh.itdiplom.dbsw.bari.domain.BariUser;
<span class="line-number">  5</span> <span class="keyword-directive">import</span> dk.jsh.itdiplom.dbsw.bari.domain.DiscussionMessage;
<span class="line-number">  6</span> <span class="keyword-directive">import</span> java.util.ArrayList;
<span class="line-number">  7</span> <span class="keyword-directive">import</span> java.util.Date;
<span class="line-number">  8</span> <span class="keyword-directive">import</span> java.util.List;
<span class="line-number">  9</span> <span class="keyword-directive">import</span> org.apache.wicket.AttributeModifier;
<span class="line-number"> 10</span> <span class="keyword-directive">import</span> org.apache.wicket.Page;
<span class="line-number"> 11</span> <span class="keyword-directive">import</span> org.apache.wicket.markup.html.basic.Label;
<span class="line-number"> 12</span> <span class="keyword-directive">imp</span><span class="keyword-directive">ort</span> org.apache.wicket.markup.html.form.Button;
<span class="line-number"> 13</span> <span class="keyword-directive">import</span> org.apache.wicket.markup.html.form.Form;
<span class="line-number"> 14</span> <span class="keyword-directive">import</span> org.apache.wicket.markup.html.form.TextArea;
<span class="line-number"> 15</span> <span class="keyword-directive">import</span> org.apache.wicket.markup.html.form.TextField;
<span class="line-number"> 16</span> <span class="keyword-directive">import</span> org.apache.wicket.markup.html.link.Link;
<span class="line-number"> 17</span> <span class="keyword-directive">import</span> org.apache.wicket.model.Model;
<span class="line-number"> 18</span> 
<span class="line-number"> 19</span> <span class="comment">/**</span>
<span class="line-number"> 20</span> <span class="comment"> * </span><span class="comment">Discussion</span> <span class="comment">page</span><span class="comment">.</span>
<span class="line-number"> 21</span> <span class="comment"> * </span>
<span class="line-number"> 22</span> <span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">Ja</span><span class="comment">n</span> <span class="comment">S</span><span class="comment">.</span> <span class="comment">Hansen</span>
<span class="line-number"> 23</span>  <span class="comment">*/</span>
<span class="line-number"> 24</span> <span class="keyword-directive">public</span> <span class="keyword-directive">final</span> <span class="keyword-directive">class</span> Discussion <span class="keyword-directive">extends</span> BasePage {
<span class="line-number"> 25</span>     <span class="keyword-directive">private</span> TextField&lt;String&gt; user;
<span class="line-number"> 26</span>     <span class="keyword-directive">private</span> TextArea&lt;String&gt; message;
<span class="line-number"> 27</span> 
<span class="line-number"> 28</span>     <span class="comment">/**</span>
<span class="line-number"> 29</span> <span class="comment">     * </span><span class="comment">Constructor</span><span class="comment">.</span>
<span class="line-number"> 30</span> <span class="comment">     * </span>
<span class="line-number"> 31</span> <span class="comment">     * </span><span class="ST0">@param</span> <span class="comment">bariCase</span> <span class="comment">A</span> <span class="comment">BaRICase</span><span class="comment">.</span>
<span class="line-number"> 32</span>      <span class="comment">*/</span>
<span class="line-number"> 33</span>     <span class="keyword-directive">public</span> Discussion(<span class="keyword-directive">final</span> BariCase bariCase) {
<span class="line-number"> 34</span>         add(<span class="keyword-directive">new</span> Label(<span class="character">&quot;</span><span class="character">product</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> Model(bariCase.getProduct().getName())));
<span class="line-number"> 35</span>         add(<span class="keyword-directive">new</span> Label(<span class="character">&quot;</span><span class="character">type</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> Model(bariCase.getType().getDescription())));
<span class="line-number"> 36</span>         add(<span class="keyword-directive">new</span> Label(<span class="character">&quot;</span><span class="character">title</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> Model(bariCase.getTitle())));
<span class="line-number"> 37</span> 
<span class="line-number"> 38</span>         <span class="comment">//Get all discussion messages and build a discussion log.</span>
<span class="line-number"> 39</span>         List&lt;DiscussionMessage&gt; discussionMessages =
<span class="line-number"> 40</span>                 DiscussionMessageBusiness.getAllDiscussionMessages(bariCase);
<span class="line-number"> 41</span>         <span class="keyword-directive">if</span> (discussionMessages.size() &gt; 0) {
<span class="line-number"> 42</span>             StringBuilder log = <span class="keyword-directive">new</span> StringBuilder();
<span class="line-number"> 43</span>             <span class="keyword-directive">for</span> (DiscussionMessage discussionMessage : discussionMessages) {
<span class="line-number"> 44</span>                 log.append(standardDateTimeFormat.format(
<span class="line-number"> 45</span>                         discussionMessage.getCreated()));
<span class="line-number"> 46</span>                 log.append(<span class="character">&quot;</span><span class="character"> af </span><span class="character">&quot;</span>);
<span class="line-number"> 47</span>                 log.append(discussionMessage.getBariUser().getFullname());
<span class="line-number"> 48</span>                 log.append(<span class="character">&quot;</span><span class="character">:</span><span class="ST1">\n</span><span class="character">&quot;</span>);
<span class="line-number"> 49</span>                 log.append(discussionMessage.getMessage());
<span class="line-number"> 50</span>                 log.append(<span class="character">&quot;</span><span class="ST1">\n</span><span class="ST1">\n</span><span class="character">&quot;</span>);
<span class="line-number"> 51</span>             }
<span class="line-number"> 52</span>             add(<span class="keyword-directive">new</span> TextArea(<span class="character">&quot;</span><span class="character">log</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> Model(log.toString())));
<span class="line-number"> 53</span>         }
<span class="line-number"> 54</span>         <span class="keyword-directive">else</span> {
<span class="line-number"> 55</span>             add(<span class="keyword-directive">new</span> TextArea(<span class="character">&quot;</span><span class="character">log</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> Model(<span class="character">&quot;</span><span class="character">Der er ingen indlæg.</span><span class="character">&quot;</span>)));
<span class="line-number"> 56</span>         }
<span class="line-number"> 57</span> 
<span class="line-number"> 58</span>         <span class="comment">//Create and add a form</span>
<span class="line-number"> 59</span>         Form form = <span class="keyword-directive">new</span> Form(<span class="character">&quot;</span><span class="character">form</span><span class="character">&quot;</span>) {
<span class="line-number"> 60</span>             <span class="comment">//Handles required fields error.</span>
<span class="line-number"> 61</span>             @Override
<span class="line-number"> 62</span>             <span class="keyword-directive">protected</span> <span class="keyword-directive">void</span> onError() {
<span class="line-number"> 63</span>                 List&lt;String&gt; emptyFields = <span class="keyword-directive">new</span> ArrayList&lt;String&gt;();
<span class="line-number"> 64</span>                 <span class="keyword-directive">if</span> (!message.checkRequired()) {
<span class="line-number"> 65</span>                     emptyFields.add(<span class="character">&quot;</span><span class="character">Indlæg</span><span class="character">&quot;</span>);
<span class="line-number"> 66</span>                     message.add(<span class="keyword-directive">n</span><span class="keyword-directive">ew</span> AttributeModifier(<span class="character">&quot;</span><span class="character">style</span><span class="character">&quot;</span>, <span class="keyword-directive">true</span>,
<span class="line-number"> 67</span>                             <span class="keyword-directive">new</span> Model(<span class="character">&quot;</span><span class="character">border-color:red;</span><span class="character">&quot;</span>)));
<span class="line-number"> 68</span>                 }
<span class="line-number"> 69</span>                 <span class="keyword-directive">else</span> {
<span class="line-number"> 70</span>                     message.add(<span class="keyword-directive">new</span> AttributeModifier(<span class="character">&quot;</span><span class="character">style</span><span class="character">&quot;</span>, <span class="keyword-directive">true</span>,
<span class="line-number"> 71</span>                             <span class="keyword-directive">new</span> Model(<span class="character">&quot;</span><span class="character">border-color:default;</span><span class="character">&quot;</span>)));
<span class="line-number"> 72</span>                 }
<span class="line-number"> 73</span>                 StringBuilder errorMessage = <span class="keyword-directive">new</span> StringBuilder();
<span class="line-number"> 74</span>                 <span class="keyword-directive">if</span> (emptyFields.size() == 1) {
<span class="line-number"> 75</span>                     errorMessage.append(<span class="character">&quot;</span><span class="character">Feltet </span><span class="character">&quot;</span>);
<span class="line-number"> 76</span>                     errorMessage.append(<span class="character">&quot;</span><span class="character">&#39;</span><span class="character">&quot;</span>).append(emptyFields.get(0))
<span class="line-number"> 77</span>                             .append(<span class="character">&quot;</span><span class="character">&#39;</span><span class="character">&quot;</span>);
<span class="line-number"> 78</span>                 }
<span class="line-number"> 79</span>                 <span class="keyword-directive">else</span> {
<span class="line-number"> 80</span>                     errorMessage.append(<span class="character">&quot;</span><span class="character">Felt</span><span class="character">erne </span><span class="character">&quot;</span>);
<span class="line-number"> 81</span>                     <span class="keyword-directive">int</span> fieldCounter = 1;
<span class="line-number"> 82</span>                     <span class="keyword-directive">for</span> (String field : emptyFields) {
<span class="line-number"> 83</span>                         errorMessage.append(<span class="character">&quot;</span><span class="character">&#39;</span><span class="character">&quot;</span>).append(field).append(<span class="character">&quot;</span><span class="character">&#39;</span><span class="character">&quot;</span>);
<span class="line-number"> 84</span>                         <span class="keyword-directive">if</span> (fieldCounter &lt; emptyFields.size() -1) {
<span class="line-number"> 85</span>                             errorMessage.append(<span class="character">&quot;</span><span class="character">, </span><span class="character">&quot;</span>);
<span class="line-number"> 86</span>                         }
<span class="line-number"> 87</span>                         <span class="keyword-directive">if</span> (fieldCounter == emptyFields.size() -1) {
<span class="line-number"> 88</span>                             errorMessage.append(<span class="character">&quot;</span><span class="character"> og </span><span class="character">&quot;</span>);
<span class="line-number"> 89</span>                         }
<span class="line-number"> 90</span>                         fieldCounter++;
<span class="line-number"> 91</span>                     }
<span class="line-number"> 92</span>                 }
<span class="line-number"> 93</span>                 errorMessage.append(<span class="character">&quot;</span><span class="character"> skal udfyl</span><span class="character">des.</span><span class="character">&quot;</span>);
<span class="line-number"> 94</span>                 setErrorMessage(errorMessage.toString());
<span class="line-number"> 95</span>             }
<span class="line-number"> 96</span>         };
<span class="line-number"> 97</span>         add(form);
<span class="line-number"> 98</span> 
<span class="line-number"> 99</span>         <span class="comment">//Add fields to t</span><span class="comment">he form.</span>
<span class="line-number">100</span>         message = <span class="keyword-directive">new</span> TextArea(<span class="character">&quot;</span><span class="character">message</span><span class="character">&quot;</span>, <span class="keyword-directive">new</span> Model(<span class="character">&quot;&quot;</span>));
<span class="line-number">101</span>         message.setRequired(<span class="keyword-directive">true</span>);
<span class="line-number">102</span>         form.add(message);
<span class="line-number">103</span> 
<span class="line-number">104</span>         <span class="comment">//Add links and buttons to the form.</span>
<span class="line-number">105</span>         form.add(<span class="keyword-directive">new</span> Link(<span class="character">&quot;</span><span class="character">goBack</span><span class="character">&quot;</span>) {
<span class="line-number">106</span>             @Override
<span class="line-number">107</span>             <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> onClick() {
<span class="line-number">108</span>                 Page page = <span class="keyword-directive">new</span> Update(bariCase);
<span class="line-number">109</span>                 setResponsePage(page);
<span class="line-number">110</span>             }
<span class="line-number">111</span>         });
<span class="line-number">112</span> 
<span class="line-number">113</span>         form.add(<span class="keyword-directive">new</span> Button(<span class="character">&quot;</span><span class="character">save</span><span class="character">&quot;</span>) {
<span class="line-number">114</span>             @Override
<span class="line-number">115</span>             <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> onSubmit() {
<span class="line-number">116</span>                 BariUser bariUser = BariSession.get().getBariUser();
<span class="line-number">117</span>                 DiscussionMessage discussionMessage = <span class="keyword-directive">new</span> DiscussionMessage(
<span class="line-number">118</span>                         bariCase, <span class="keyword-directive">new</span> Date(), bariUser,
<span class="line-number">119</span>                         message.getModelObject());
<span class="line-number">120</span>                 DiscussionMessageBusiness.saveNew(discussionMessage);
<span class="line-number">121</span>                 Page page = <span class="keyword-directive">new</span> Discussion(bariCase);
<span class="line-number">122</span>                 setResponsePage(page);
<span class="line-number">123</span>             }
<span class="line-number">124</span>       });
<span class="line-number">125</span>     }
<span class="line-number">126</span> }
<span class="line-number">127</span> 
</pre></body>
</html>
